package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.SafetyBean;
import com.hengxinguotong.zhihuichengjian.bean.SafetyResultsBean;
import com.hengxinguotong.zhihuichengjian.ui.security.SafetyCheckResultsActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/27.
 */

public class SafetyResultsAdapter extends BaseExpandableListAdapter {

    private List<SafetyBean> resultsList;
//    private int groupSelect = -1;
//    private int childSelect = -1;
    private LayoutInflater inflater;
    private SafetyCheckResultsActivity mSafetyCheckResultsActivity;

    public SafetyResultsAdapter(List<SafetyBean> resultsList, Context context) {
        this.resultsList = resultsList;
        this.inflater = LayoutInflater.from(context);
        this.mSafetyCheckResultsActivity = (SafetyCheckResultsActivity)context;
    }

    @Override
    public int getGroupCount() {
        return resultsList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return resultsList.get(groupPosition).getData().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return resultsList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return resultsList.get(groupPosition).getData().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_results_group, null);
        }
        HXTextView group_name = (HXTextView) convertView.findViewById(R.id.group_name);
        group_name.setText(resultsList.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_result_child, null);
        }
        HXTextView nameTv = (HXTextView) convertView.findViewById(R.id.child_name);
        ImageView checkBox = (ImageView) convertView.findViewById(R.id.checkbox);
        SafetyBean safetyBean = resultsList.get(groupPosition).getData().get(childPosition);
        nameTv.setText(safetyBean.getName());
        if (mSafetyCheckResultsActivity.getmSelectedSafetyList().contains(safetyBean)) {
            checkBox.setVisibility(View.VISIBLE);
        } else {
            checkBox.setVisibility(View.GONE);
        }
        return convertView;
    }

//    public SafetyBean getSelect() {
//        if (groupSelect == -1) {
//            return null;
//        }
//        if (childSelect == -1) {
//            return null;
//        }
//        return resultsList.get(groupSelect).getData().get(childSelect);
//    }
//
//    public void setSelect(int groupSelect, int childSelect) {
//        this.groupSelect = groupSelect;
//        this.childSelect = childSelect;
//        notifyDataSetChanged();
//    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
